#include "hrvana.h"
#include "qcustomplot.h"
#include "ui_hrvana.h"
#include "HRVCompute/freqDomainHRV.h"
#include "HRVCompute/nonlinearHRV.h"
#include "HRVCompute/poincareHRV.h"
#include "HRVCompute/timeDomainHRV.h"
#include "HRVCompute/timeFreqHRV.h"
#include "Utils/readfiles.h"

#include <Utils/dataconvert.h>
#include <Utils/psdcurvesdrawutil.h>
#include <Utils/singlecurvedraw.h>
#include <Utils/spectrogramplot.h>
#include <Utils/tfdataconvert.h>
#include <Utils/timefreqspectrogramplot.h>

HRVAna::HRVAna(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::HRVAna)
{
    ui->setupUi(this);
    initQcustomPlots();
    init3DSurf();
    initControls();
    mentPressureComp=new MentPressureComp;
    mentPressureComp->SetUserInfo(28,0);
}

HRVAna::~HRVAna()
{
    delete ui;
}

void HRVAna::on_pushButton_exit_clicked()
{
    this->close();
}


void HRVAna::on_pushButton_ReadRawRR_clicked()
{
    ReadRawRRData();
    ui->pushButton_startComp->setEnabled(true);
}

void HRVAna::initQcustomPlots()
{
    initRRCurve();
    initScatter();
    initDFACurve();
}
void HRVAna::initControls()
{
    ui->radioButton_EctopicRep_None->setChecked(true);
    ui->lineEdit_EctopicD_p->setValidator(new QIntValidator(1,20,this));
    ui->lineEdit_EctopicD_std->setValidator(new QIntValidator(1,20,this));
    ui->lineEdit_EctopicD_median->setValidator(new QIntValidator(1,20,this));
    ui->lineEdit_EctopicRep_mean->setValidator(new QIntValidator(1,20,this));
    ui->lineEdit_EctopicRep_median->setValidator(new QIntValidator(1,20,this));
    // 在窗口类中初始化
    ui->lineEdit_VLFL->setValidator(new QDoubleValidator(this));
    ui->lineEdit_LFL->setValidator(new QDoubleValidator(this));
    ui->lineEdit_HFL->setValidator(new QDoubleValidator(this));
    ui->lineEdit_VLFH->setValidator(new QDoubleValidator(this));
    ui->lineEdit_LFH->setValidator(new QDoubleValidator(this));
    ui->lineEdit_HFH->setValidator(new QDoubleValidator(this));

    ui->lineEdit_AROrder->setValidator(new QIntValidator(1,32,this));
    ui->lineEdit_InterpolationRate->setValidator(new QIntValidator(2,4,this));
    ui->lineEdit_PSDpoints->setValidator(new QIntValidator(256,2048,this));
    ui->lineEdit_WelchWidth->setValidator(new QIntValidator(64,129,this));
    ui->lineEdit_WelchWindowOverlap->setValidator(new QIntValidator(16,64,this));

    ui->lineEdit_Window->setValidator(new QIntValidator(10,180,this));
    ui->lineEdit_Overlap->setValidator(new QIntValidator(5,60,this));

    ui->lineEdit_SampEn_r->setValidator(new QDoubleValidator(this));
    ui->lineEdit_SampEn_m->setValidator(new QDoubleValidator(this));
    ui->lineEdit_DFA_L->setValidator(new QIntValidator(0,10,this));
    ui->lineEdit_DFA_H->setValidator(new QIntValidator(50,100,this));
    ui->lineEdit_DFA_breakPoint->setValidator(new QIntValidator(0,64,this));
    timeFreqSpectrogramPlot_burg=new TimeFreqSpectrogramPlot("Burg Spectrogram",ui->widget_burg);
    timeFreqSpectrogramPlot_lomb=new TimeFreqSpectrogramPlot("Lomb Spectrogram",ui->widget_LS);
    timeFreqSpectrogramPlot_wavelet=new TimeFreqSpectrogramPlot("Wavelet Spectrogram",ui->widget_Wavlet);
    ui->comboBox_surf->setVisible(false);
    ui->tabWidget_2->setCurrentIndex(0);
    ui->tabWidget->setCurrentIndex(0);
    ui->pushButton_startComp->setEnabled(false);


}
void HRVAna::initRRCurve()
{
    QCPGraph *graph = ui->widget_RR->addGraph();
    graph->setLineStyle(QCPGraph::lsNone); // 启用线条连接
    graph->setScatterStyle(QCPScatterStyle(QCPScatterStyle::ssDisc,4)); // 设置点的样式为圆圈
    // graph->scatterStyle().setSize(0.1);
    ui->widget_RR->graph(0)->setPen(QPen(QColor(0x2f,0x76,0xe0))); // 设置初始曲线颜色
    ui->widget_RR->graph(0)->setName("RR间期");
    // 启用图例
    ui->widget_RR->legend->setVisible(true);
    // 调整图例大小
    ui->widget_RR->legend->setAntialiased(true);
    // ui->widget_RR->legend->setMinimumSize(150,  50); // 设置最小尺寸
    // ui->widget_RR->legend->setMaximumSize(200,  100); // 设置最大尺寸
    // 设置字体大小
    QFont legendFont = ui->widget_RR->legend->font();
    legendFont.setPointSize(10);  // 设置字体大小为 12
    ui->widget_RR->legend->setFont(legendFont);
    // 设置图表名称
    QCPTextElement *title = new QCPTextElement(ui->widget_RR); // 创建标题元素
    title->setText("RR间期时域波形图");                              // 设置标题文本
    title->setFont(QFont("sans", 12, QFont::Bold));         // 设置字体
    ui->widget_RR->plotLayout()->insertRow(0);                   // 在顶部插入一行
    ui->widget_RR->plotLayout()->addElement(0,  0, title);        // 将标题添加到布局中
    // 将图例放到图表右侧外面
    // 创建一个新的QCPLayoutGrid来放置图例
    QCPLayoutGrid* subLayout = new QCPLayoutGrid;
    ui->widget_RR->plotLayout()->addElement(2, 0, subLayout); // 将subLayout添加到主布局的指定位置
    // 设置subLayout的边距
    subLayout->setMargins(QMargins(0, 0, 1000, 5));
    // 将图例添加到subLayout中
    subLayout->addElement(0, 0, ui->widget_RR->legend);
    // 调整图例的填充顺序（可选）
    ui->widget_RR->legend->setFillOrder(QCPLegend::foColumnsFirst);
    // 设置图例所在行的拉伸因子，以控制其高度
    ui->widget_RR->plotLayout()->setRowStretchFactor(2, 0.001);
    // 重绘图表以显示更改
    ui->widget_RR->replot();
    // 启用交互功能
    ui->widget_RR->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
    ui->widget_RR->setSelectionRectMode(QCP::SelectionRectMode::srmZoom);
    // ui->widget_RR->setInteraction(QCP::iRangeDrag, true);
    ui->widget_RR->setNoAntialiasingOnDrag(true);
    // 设置坐标轴标签
    ui->widget_RR->xAxis->setLabel("Time(S)");
    ui->widget_RR->yAxis->setLabel("IBI(S)");
    ui->widget_RR->yAxis->setRange(0.4, 1.4);

    //两根标准差线
    ui->widget_RR->addGraph();
    ui->widget_RR->graph(1)->setName("均值+3倍标准差");
    QPen pen;
    pen.setStyle(Qt::DashLine);  // 设置为虚线
    pen.setColor(Qt::red);      // 设置线条颜色
    pen.setWidth(1);             // 设置线条宽度

    ui->widget_RR->graph(1)->setPen(pen); // 设置初始曲线颜色
    ui->widget_RR->addGraph();
    ui->widget_RR->graph(2)->setPen(pen); // 设置初始曲线颜色
    ui->widget_RR->graph(2)->setName("均值-3倍标准差");
    //异常点
    QCPGraph *graph2 = ui->widget_RR->addGraph();
    graph2->setLineStyle(QCPGraph::lsNone); // 启用线条连接
    graph2->setScatterStyle(QCPScatterStyle::ssCircle); // 设置点的样式为圆圈
    ui->widget_RR->graph(3)->setPen(QPen(Qt::red,2)); // 设置初始曲线颜色
    ui->widget_RR->graph(3)->setName("异常rr间期");

    //替换点
    QCPGraph *graph3 = ui->widget_RR->addGraph();
    graph3->setLineStyle(QCPGraph::lsNone); // 启用线条连接
    graph3->setScatterStyle(QCPScatterStyle::ssDiamond); // 设置点的样式为圆圈
    ui->widget_RR->graph(4)->setPen(QPen(Qt::green,2)); // 设置初始曲线颜色
    ui->widget_RR->graph(4)->setName("替换rr间期");

}

/**
 * @brief HRVAna::drawScatter
 * @param proRRData_y
 */
void HRVAna::initScatter()
{
    QCPGraph *graph = ui->widget_scatter->addGraph();
    graph->setLineStyle(QCPGraph::lsNone); // 启用线条连接
    graph->setScatterStyle(QCPScatterStyle::ssCircle); // 设置点的样式为圆圈
    ui->widget_scatter->xAxis->setLabel("IBI_N (s)");
    ui->widget_scatter->yAxis->setLabel("IBI_N+1 (s)");
    ui->widget_scatter->graph(0)->setPen(QPen(QColor(0x2f,0x76,0xe0))); // 设置初始曲线颜色
    ui->widget_scatter->graph(0)->setName("RR间期散点图");
    // 设置坐标轴范围
    ui->widget_scatter->xAxis->setRange(0.4, 1.2);
    ui->widget_scatter->yAxis->setRange(0.4, 1.2);
    QCPTextElement *title = new QCPTextElement(ui->widget_scatter); // 创建标题元素
    title->setText("散点图");                              // 设置标题文本
    title->setFont(QFont("sans", 12, QFont::Bold));         // 设置字体
    ui->widget_scatter->plotLayout()->insertRow(0);                   // 在顶部插入一行
    ui->widget_scatter->plotLayout()->addElement(0,  0, title);        // 将标题添加到布局中
    ui->widget_scatter->replot(); // 刷新绘图
}


void HRVAna::initDFACurve()
{
    QCPGraph *graph = ui->widget_dfa->addGraph();
    graph->setLineStyle(QCPGraph::lsNone); // 启用线条连接
    graph->setScatterStyle(QCPScatterStyle::ssCircle); // 设置点的样式为圆圈
    ui->widget_dfa->graph(0)->setPen(QPen(QColor(0x2f,0x76,0xe0))); // 设置初始曲线颜色
    ui->widget_dfa->graph(0)->setName("DFA");
    // 启用图例
    ui->widget_dfa->legend->setVisible(true);
    // 调整图例大小
    ui->widget_dfa->legend->setAntialiased(true);
    // ui->widget_RR->legend->setMinimumSize(150,  50); // 设置最小尺寸
    // ui->widget_RR->legend->setMaximumSize(200,  100); // 设置最大尺寸
    // 设置字体大小
    QFont legendFont = ui->widget_dfa->legend->font();
    legendFont.setPointSize(10);  // 设置字体大小为 12
    ui->widget_dfa->legend->setFont(legendFont);
    // 设置图表名称
    QCPTextElement *title = new QCPTextElement(ui->widget_dfa); // 创建标题元素
    title->setText("DFA");                              // 设置标题文本
    title->setFont(QFont("sans", 12, QFont::Bold));         // 设置字体
    ui->widget_dfa->plotLayout()->insertRow(0);                   // 在顶部插入一行
    ui->widget_dfa->plotLayout()->addElement(0,  0, title);        // 将标题添加到布局中
    // 将图例放到图表右侧外面
    // 创建一个新的QCPLayoutGrid来放置图例
    QCPLayoutGrid* subLayout = new QCPLayoutGrid;
    ui->widget_dfa->plotLayout()->addElement(2, 0, subLayout); // 将subLayout添加到主布局的指定位置
    // 设置subLayout的边距
    subLayout->setMargins(QMargins(0, 0, 500, 5));
    // 将图例添加到subLayout中
    subLayout->addElement(0, 0, ui->widget_dfa->legend);
    // 调整图例的填充顺序（可选）
    ui->widget_dfa->legend->setFillOrder(QCPLegend::foColumnsFirst);
    // 设置图例所在行的拉伸因子，以控制其高度
    ui->widget_dfa->plotLayout()->setRowStretchFactor(2, 0.001);
    // 重绘图表以显示更改
    ui->widget_dfa->replot();
    // 启用交互功能
    ui->widget_dfa->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
    ui->widget_dfa->setSelectionRectMode(QCP::SelectionRectMode::srmZoom);
    // ui->widget_RR->setInteraction(QCP::iRangeDrag, true);
    ui->widget_dfa->setNoAntialiasingOnDrag(true);
    // 设置坐标轴标签
    ui->widget_dfa->xAxis->setLabel("log10(n)");
    ui->widget_dfa->yAxis->setLabel("log10 F(n)");
    // ui->widget_dfa->yAxis->setRange(400, 1400);

    //两根标准差线
    ui->widget_dfa->addGraph();
    ui->widget_dfa->graph(1)->setName("小时间尺度-短程标度指数趋势图");
    QPen pen;
    pen.setStyle(Qt::SolidLine);  // 设置为虚线
    pen.setColor(Qt::red);      // 设置线条颜色
    pen.setWidth(3);             // 设置线条宽度

    ui->widget_dfa->graph(1)->setPen(pen); // 设置初始曲线颜色
    ui->widget_dfa->addGraph();
    pen.setColor(Qt::green);
    ui->widget_dfa->graph(2)->setPen(pen); // 设置初始曲线颜色
    ui->widget_dfa->graph(2)->setName("大时间尺度-长程标度指数趋势图");


}

void HRVAna::init3DSurf()
{
    //3Dfft频谱图
    graph = new Q3DSurface();
    container = QWidget::createWindowContainer(graph);
    ui->verticalLayout_surf->addWidget(container,1);
    modifier = new SurfaceGraphModifier(graph);
}
/**
 * @brief HRVAna::initData
 * 读取原始rr间期数据
 */
void HRVAna::ReadRawRRData()
{
    rawRRData_x.clear();
    rawRRData_y.clear();
    Ectopic_b.clear();
    proRRData_x.clear();
    proRRData_y.clear();
    ReadFiles readfiles;
    if (!readfiles.readtxtFile("E:/qtCode/heart-language-laboratory/TestMatlabLib/TestMatlabLib/nibi.txt",  rawRRData_x, rawRRData_y)) {
        // 处理错误
    } else {
        rawrr_Len=rawRRData_y.size();
        UpdateRRGraph();
    }
}

void HRVAna::UpdateRRGraph()
{
    int len=rawRRData_x.size();
    qDebug()<<"rawRRData_len="<<len;
    if(len>0)
    {
        ui->widget_RR->graph(0)->setData(rawRRData_x,rawRRData_y);
        ui->widget_RR->xAxis->setRange(0,rawRRData_x.at(len-1));
        double meanRR=ArrayOperation::instance().calculateMean(rawRRData_y);
        double stdRR=ArrayOperation::instance().calculateStdDev(rawRRData_y);
        QVector<double>m_std_low_x,m_std_up_x,m_std_low_y,m_std_up_y;
        m_std_low_x.append(0);m_std_low_x.append(rawRRData_x.at(len-1));
        m_std_up_x.append(0);m_std_up_x.append(rawRRData_x.at(len-1));
        m_std_low_y.append(meanRR-3*stdRR);m_std_low_y.append(meanRR-3*stdRR);
        m_std_up_y.append(meanRR+3*stdRR);m_std_up_y.append(meanRR+3*stdRR);
        ui->widget_RR->graph(1)->setData(m_std_low_x,m_std_low_y);
        ui->widget_RR->graph(2)->setData(m_std_up_x,m_std_up_y);

        int ectopic_len=Ectopic_b.size();
        //异常点位置获取
        QVector<double>ectopic_x,ectopic_y;
        for(int i=0;i<ectopic_len;i++)
        {
            if(Ectopic_b.at(i))
            {
                ectopic_x.append(rawRRData_x.at(i));
                ectopic_y.append(rawRRData_y.at(i));
            }
        }
        ui->widget_RR->graph(3)->setData(ectopic_x,ectopic_y);
        //异常点替换获取
        QVector<double>replace_ectopic_x,replace_ectopic_y;
        if(ui->radioButton_EctopicRep_None->isChecked()){

        }else if(ui->radioButton_EctopicRep_ReMove->isChecked()){
            //删除后的数据，即保留的数据
            ui->widget_RR->graph(4)->setData(proRRData_x,proRRData_y);
            ui->widget_RR->graph(4)->setName("删除后rr间期");
            ui->widget_RR->graph(4)->setPen(QPen(Qt::darkGray,1)); // 设置初始曲线颜色
        }else{
            for(int i=0;i<ectopic_len;i++)
            {
                if(Ectopic_b.at(i))
                {
                    replace_ectopic_x.append(proRRData_x.at(i));
                    replace_ectopic_y.append(proRRData_y.at(i));
                }
            }
            ui->widget_RR->graph(4)->setName("替换rr间期");
            ui->widget_RR->graph(4)->setPen(QPen(Qt::green,2)); // 设置初始曲线颜色
            ui->widget_RR->graph(4)->setData(replace_ectopic_x,replace_ectopic_y);
        }
        ui->widget_RR->replot();// 刷新图表
    }


}
void HRVAna::on_pb_TimeDomain_clicked()
{
    ui->tabWidget->setCurrentIndex(0);
}


void HRVAna::on_pb_FreqDomain_clicked()
{
    ui->tabWidget->setCurrentIndex(1);
}

void HRVAna::on_pb_Scatter_clicked()
{
    ui->tabWidget->setCurrentIndex(2);
}

void HRVAna::on_pb_NonLinearDomain_clicked()
{
    ui->tabWidget->setCurrentIndex(3);
}

void HRVAna::on_pb_TimeFreqDomain_clicked()
{
    ui->tabWidget->setCurrentIndex(4);
}

void HRVAna::on_pb_MentalPressure_clicked()
{
    ui->tabWidget->setCurrentIndex(5);

}

void HRVAna::on_pb_Nerval_clicked()
{
    ui->tabWidget->setCurrentIndex(6);
}
void HRVAna::on_pushButton_startComp_clicked()
{
    bool b_p=ui->checkBox_EctopicD_p->isChecked();
    bool b_sd=ui->checkBox_EctopicD_std->isChecked();
    bool b_median=ui->checkBox_EctopicD_median->isChecked();
    qDebug()<<"startComp-1";
    EctopicPro(b_p,b_sd,b_median);
    qDebug()<<"startComp-2";
    ReplaceEctopicPro();
    qDebug()<<"startComp-3";
    UpdateRRGraph();

    //开始计算hrv
    qDebug()<<"startComp-4";
    ComputeHRV();
    qDebug()<<"startComp-5";
}

coder::array<double, 2U> HRVAna::transData(QVector<double>proRR_x,QVector<double>proRR_y,bool isSecond)
{
    coder::array<double, 2U> ibi;
    int len=proRR_x.size();
    qDebug()<<"transData_len="<<len;
    ibi.set_size(len, 2);
    int v=isSecond?1000:1;
    for (int idx0{0}; idx0 < len; idx0++) {
        for (int idx1{0}; idx1 < 2; idx1++) {
            if(idx1==0)
                ibi[idx0 + len * idx1] = proRR_x.at(idx0);
            else
                ibi[idx0 + len * idx1] = proRR_y.at(idx0)*v;
        }
    }
    return ibi;
}



void HRVAna::ComputeHRV()
{
    qDebug()<<"ComputeHRV()1";
    ui->tabWidget->setCurrentIndex(0);
    coder::array<double, 2U>ibi=transData(proRRData_x,proRRData_y,false);
    struct8_T output_time;
    timeDomainHRV(ibi, 60, 50, &output_time);
    ui->label_MeanRR->setText(QString::number(output_time.mean,'f',1));
    ui->label_SDNN->setText(QString::number(output_time.SDNN,'f',2));
    ui->label_MeanHR->setText(QString::number(output_time.meanHR,'f',1));
    ui->label_SDHR->setText(QString::number(output_time.sdHR,'f',1));
    ui->label_RMSSD->setText(QString::number(output_time.RMSSD,'f',1));
    ui->label_NNx->setText(QString::number(output_time.NNx,'f',0));
    ui->label_pNNx->setText(QString::number(output_time.pNNx,'f',1));
    ui->label_SDNNi->setText(QString::number(output_time.SDNNi,'f',1));
    ui->label_SDNN->setText(QString::number(output_time.SDNN,'f',1));
    ui->label_HRVTi->setText(QString::number(output_time.HRVTi,'f',1));
    ui->label_TINN->setText(QString::number(output_time.TINN,'f',1));
    qDebug()<<"ComputeHRV()2";
    ProHistogram_RR();
    qDebug()<<"ComputeHRV()3";
    ProHistogram_HR();
    struct0_T output_freq;
    double vlf_l=ui->lineEdit_VLFL->text().toDouble();
    double vlf_h=ui->lineEdit_VLFH->text().toDouble();
    double lf_l=ui->lineEdit_LFL->text().toDouble();
    double lf_h=ui->lineEdit_LFH->text().toDouble();
    double hf_l=ui->lineEdit_HFL->text().toDouble();
    double hf_h=ui->lineEdit_HFH->text().toDouble();
    int ar_order=ui->lineEdit_AROrder->text().toInt();

    const double VLF_tmp[2]={vlf_l,vlf_h};
    const double LF_tmp[2]={lf_l,lf_h};
    const double HF_tmp[2]={hf_l,hf_h};
    int InterpolationRate=ui->lineEdit_InterpolationRate->text().toInt();
    int PSDpoints=ui->lineEdit_PSDpoints->text().toInt();
    int WelchWidth=ui->lineEdit_WelchWidth->text().toInt();
    int WelchWindowOverlap=ui->lineEdit_WelchWindowOverlap->text().toInt();

    qDebug()<<"ComputeHRV()4";
    freqDomainHRV(ibi, VLF_tmp, LF_tmp, HF_tmp, ar_order, WelchWidth,WelchWindowOverlap, PSDpoints, InterpolationRate, &output_freq);

    qDebug()<<"ComputeHRV()5";
    DataConvert dc;
    QVector<double>welch_f=dc.convertToQVector(output_freq.welch.f);
    QVector<QVector<double>>welch_psd=dc.convertRowMajor(output_freq.welch.psd);

    QVector<double>ar_f=dc.convertToQVector(output_freq.ar.f);
    QVector<double>ar_psd=dc.convertToQVector(output_freq.ar.psd);

    QVector<QVector<double>>lomb_f=dc.convertRowMajor(output_freq.lomb.f);
    QVector<QVector<double>>lomb_psd=dc.convertRowMajor(output_freq.lomb.psd);


    QVector<double>welch_f_vlf,welch_f_lf,welch_f_hf;
    QVector<double>welch_psd_vlf,welch_psd_lf,welch_psd_hf;

    QVector<double>ar_f_vlf,ar_f_lf,ar_f_hf;
    QVector<double>ar_psd_vlf,ar_psd_lf,ar_psd_hf;

    QVector<double>lomb_f_vlf,lomb_f_lf,lomb_f_hf;
    QVector<double>lomb_psd_vlf,lomb_psd_lf,lomb_psd_hf;

    for(int i=0;i<PSDpoints;i++)
    {
        if(welch_f.at(i)<vlf_h){
            welch_f_vlf.append(welch_f.at(i));
            welch_psd_vlf.append(welch_psd.at(i).at(0));
            ar_f_vlf.append(ar_f.at(i));
            ar_psd_vlf.append(ar_psd.at(i));
            lomb_f_vlf.append(lomb_f.at(0).at(i));
            lomb_psd_vlf.append(lomb_psd.at(0).at(i));
        }
        if(welch_f.at(i)>=lf_l && welch_f.at(i)<lf_h){
            welch_f_lf.append(welch_f.at(i));
            welch_psd_lf.append(welch_psd.at(i).at(0));

            ar_f_lf.append(ar_f.at(i));
            ar_psd_lf.append(ar_psd.at(i));

            lomb_f_lf.append(lomb_f.at(0).at(i));
            lomb_psd_lf.append(lomb_psd.at(0).at(i));
        }
        if(welch_f.at(i)>=hf_l && welch_f.at(i)<hf_h){
            welch_f_hf.append(welch_f.at(i));
            welch_psd_hf.append(welch_psd.at(i).at(0));

            ar_f_hf.append(ar_f.at(i));
            ar_psd_hf.append(ar_psd.at(i));

            lomb_f_hf.append(lomb_f.at(0).at(i));
            lomb_psd_hf.append(lomb_psd.at(0).at(i));
        }
    }

    QVector<QVector<double>> welch_fData = {welch_f_vlf, welch_f_lf,welch_f_hf};
    QVector<QVector<double>> welch_psdData = {welch_psd_vlf,welch_psd_lf,welch_psd_hf};

    QVector<QVector<double>> ar_fData = {ar_f_vlf, ar_f_lf,ar_f_hf};
    QVector<QVector<double>> ar_psdData = {ar_psd_vlf,ar_psd_lf,ar_psd_hf};

    QVector<QVector<double>> lomb_fData = {lomb_f_vlf, lomb_f_lf,lomb_f_hf};
    QVector<QVector<double>> lomb_psdData = {lomb_psd_vlf,lomb_psd_lf,lomb_psd_hf};
    qDebug()<<"ComputeHRV()6";
    // 调用绘制
    PSDCurvesDrawUtil pdu;
    pdu.drawMultiCurves(ui->widget_psd_welch,"Welch功率谱密度曲线图", welch_fData, welch_psdData);
    pdu.drawMultiCurves(ui->widget_psd_ar,"AR功率谱密度曲线图", ar_fData, ar_psdData);
    pdu.drawMultiCurves(ui->widget_psd_ls,"lomb功率谱密度曲线图", lomb_fData, lomb_psdData);


    qDebug()<<"ComputeHRV()7";
    ui->label_ar_vlfPeak->setText(QString::number(output_freq.ar.hrv.peakVLF,'f',2));
    ui->label_ar_lfPeak->setText(QString::number(output_freq.ar.hrv.peakLF,'f',2));
    ui->label_ar_hfPeak->setText(QString::number(output_freq.ar.hrv.peakHF,'f',2));
    ui->label_ar_vlfPower1->setText(QString::number(output_freq.ar.hrv.aVLF,'f',2));
    ui->label_ar_vlfPower2->setText(QString::number(output_freq.ar.hrv.pVLF,'f',1));
    ui->label_ar_lfPower1->setText(QString::number(output_freq.ar.hrv.aLF,'f',2));
    ui->label_ar_lfPower2->setText(QString::number(output_freq.ar.hrv.pLF,'f',1));
    ui->label_ar_lfPower3->setText(QString::number(output_freq.ar.hrv.nLF,'f',3));
    ui->label_ar_hfPower1->setText(QString::number(output_freq.ar.hrv.aHF,'f',2));
    ui->label_ar_hfPower2->setText(QString::number(output_freq.ar.hrv.pHF,'f',1));
    ui->label_ar_hfPower3->setText(QString::number(output_freq.ar.hrv.nHF,'f',3));
    ui->label_ar_lfhf->setText(QString::number(output_freq.ar.hrv.LFHF,'f',3));

    ui->label_welch_vlfPeak->setText(QString::number(output_freq.welch.hrv.peakVLF,'f',2));
    ui->label_welch_lfPeak->setText(QString::number(output_freq.welch.hrv.peakLF,'f',2));
    ui->label_welch_hfPeak->setText(QString::number(output_freq.welch.hrv.peakHF,'f',2));
    ui->label_welch_vlfPower1->setText(QString::number(output_freq.welch.hrv.aVLF,'f',2));
    ui->label_welch_vlfPower2->setText(QString::number(output_freq.welch.hrv.pVLF,'f',1));
    ui->label_welch_lfPower1->setText(QString::number(output_freq.welch.hrv.aLF,'f',2));
    ui->label_welch_lfPower2->setText(QString::number(output_freq.welch.hrv.pLF,'f',1));
    ui->label_welch_lfPower3->setText(QString::number(output_freq.welch.hrv.nLF,'f',3));
    ui->label_welch_hfPower1->setText(QString::number(output_freq.welch.hrv.aHF,'f',2));
    ui->label_welch_hfPower2->setText(QString::number(output_freq.welch.hrv.pHF,'f',1));
    ui->label_welch_hfPower3->setText(QString::number(output_freq.welch.hrv.nHF,'f',3));
    ui->label_welch_lfhf->setText(QString::number(output_freq.welch.hrv.LFHF,'f',3));

    ui->label_ls_vlfPeak->setText(QString::number(output_freq.lomb.hrv.peakVLF,'f',2));
    ui->label_ls_lfPeak->setText(QString::number(output_freq.lomb.hrv.peakLF,'f',2));
    ui->label_ls_hfPeak->setText(QString::number(output_freq.lomb.hrv.peakHF,'f',2));
    ui->label_ls_vlfPower1->setText(QString::number(output_freq.lomb.hrv.aVLF,'f',2));
    ui->label_ls_vlfPower2->setText(QString::number(output_freq.lomb.hrv.pVLF,'f',1));
    ui->label_ls_lfPower1->setText(QString::number(output_freq.lomb.hrv.aLF,'f',2));
    ui->label_ls_lfPower2->setText(QString::number(output_freq.lomb.hrv.pLF,'f',1));
    ui->label_ls_lfPower3->setText(QString::number(output_freq.lomb.hrv.nLF,'f',3));
    ui->label_ls_hfPower1->setText(QString::number(output_freq.lomb.hrv.aHF,'f',2));
    ui->label_ls_hfPower2->setText(QString::number(output_freq.lomb.hrv.pHF,'f',1));
    ui->label_ls_hfPower3->setText(QString::number(output_freq.lomb.hrv.nHF,'f',3));
    ui->label_ls_lfhf->setText(QString::number(output_freq.lomb.hrv.LFHF,'f',3));


    struct5_T output_non;
    double SampEn_r=ui->lineEdit_SampEn_r->text().toDouble();
    double SampEn_m=ui->lineEdit_SampEn_m->text().toDouble();
    int DFA_L=ui->lineEdit_DFA_L->text().toInt();
    int DFA_H=ui->lineEdit_DFA_H->text().toInt();
    int breakPoint=ui->lineEdit_DFA_breakPoint->text().toInt();

    nonlinearHRV(ibi, SampEn_m, SampEn_r, DFA_L, DFA_H, breakPoint, &output_non);
    qDebug()<<"ComputeHRV()8";
    QVector<double>sampEn_v=dc.convertToQVector(output_non.sampen);
    ui->label_SampEn->setText(QString::number(sampEn_v.at(2)));
    ui->label_alpha->setText(QString::number(output_non.dfa.alpha[0],'f',2));
    ui->label_alpha1->setText(QString::number(output_non.dfa.alpha1[0],'f',2));
    ui->label_alpha2->setText(QString::number(output_non.dfa.alpha2[0],'f',2));
    QVector<double>dfa_x=dc.convertToQVector(output_non.dfa.n);
    QVector<double>dfa_y=dc.convertToQVector(output_non.dfa.F_n);
    int dfa_len=dfa_x.size();
    for(int i=0;i<dfa_len;i++)
    {
        dfa_x[i]=std::log10(dfa_x.at(i));
        dfa_y[i]=std::log10(dfa_y.at(i));
    }
    ui->widget_dfa->graph(0)->setData(dfa_x,dfa_y);
    QVector<double>alpha1_x,alpha1_y,alpha2_x,alpha2_y;

    alpha1_x.append(dfa_x.at(0));
    alpha1_x.append(dfa_x.at(9));
    alpha2_x.append(dfa_x.at(10));
    alpha2_x.append(dfa_x.at(dfa_x.size()-1));

    alpha1_y.append(output_non.dfa.alpha1[0]*dfa_x.at(0)+output_non.dfa.alpha1[1]);
    alpha1_y.append(output_non.dfa.alpha1[0]*dfa_x.at(9)+output_non.dfa.alpha1[1]);
    alpha2_y.append(output_non.dfa.alpha2[0]*dfa_x.at(10)+output_non.dfa.alpha2[1]);
    alpha2_y.append(output_non.dfa.alpha2[0]*dfa_x.at(dfa_x.size()-1)+output_non.dfa.alpha2[1]);

    ui->widget_dfa->graph(1)->setData(alpha1_x,alpha1_y);
    ui->widget_dfa->graph(2)->setData(alpha2_x,alpha2_y);
    ui->widget_dfa->yAxis->setRange(dfa_y.at(0)*0.9, dfa_y.at(dfa_y.size()-1)*1.1);
    ui->widget_dfa->xAxis->setRange(dfa_x.at(0)*0.9, dfa_x.at(dfa_x.size()-1)*1.1);
    ui->widget_dfa->replot();
    struct7_T output_poin;
    poincareHRV(ibi, &output_poin);
    qDebug()<<"ComputeHRV()9";
    QVector<double>scatter_x,scatter_y;
    int len=proRRData_y.size();
    if(len>1){
        for(int i=0;i<len-1;i++)
        {
            scatter_x.append(proRRData_y.at(i));
            scatter_y.append(proRRData_y.at(i+1));
        }
        ui->widget_scatter->graph(0)->setData(scatter_x,scatter_y);
        ui->widget_scatter->replot();
    }
    ui->label_sd1->setText(QString::number(output_poin.SD1,'f',2));
    ui->label_sd2->setText(QString::number(output_poin.SD2,'f',2));
    ComputeNonLinearIndex();

    qDebug()<<"ComputeHRV()10";
    int TF_Window= ui->lineEdit_Window->text().toInt();
    int TF_Overlap=ui->lineEdit_Overlap->text().toInt();
    coder::array<double, 2U>ibi_s=transData(proRRData_x,proRRData_y,false);
    qDebug()<<"ComputeHRV()101";
    timeFreqHRV(ibi_s, VLF_tmp, LF_tmp, HF_tmp, ar_order, TF_Window,TF_Overlap, PSDpoints, InterpolationRate, &output_tf);
    qDebug()<<"ComputeHRV()102";
    ui->label_burg_vlfPeak->setText(QString::number(output_tf.ar.global.hrv.peakVLF,'f',2));
    ui->label_burg_lfPeak->setText(QString::number(output_tf.ar.global.hrv.peakLF,'f',2));
    ui->label_burg_hfPeak->setText(QString::number(output_tf.ar.global.hrv.peakHF,'f',2));
    ui->label_burg_vlfPower1->setText(QString::number(output_tf.ar.global.hrv.aVLF,'f',2));
    ui->label_burg_vlfPower2->setText(QString::number(output_tf.ar.global.hrv.pVLF,'f',1));
    ui->label_burg_lfPower1->setText(QString::number(output_tf.ar.global.hrv.aLF,'f',2));
    ui->label_burg_lfPower2->setText(QString::number(output_tf.ar.global.hrv.pLF,'f',1));
    ui->label_burg_lfPower3->setText(QString::number(output_tf.ar.global.hrv.nLF,'f',3));
    ui->label_burg_hfPower1->setText(QString::number(output_tf.ar.global.hrv.aHF,'f',2));
    ui->label_burg_hfPower2->setText(QString::number(output_tf.ar.global.hrv.pHF,'f',1));
    ui->label_burg_hfPower3->setText(QString::number(output_tf.ar.global.hrv.nHF,'f',3));
    ui->label_burg_lfhf->setText(QString::number(output_tf.ar.global.hrv.LFHF,'f',3));

    ui->label_lomb_vlfPeak->setText(QString::number(output_tf.lomb.global.hrv.peakVLF,'f',2));
    ui->label_lomb_lfPeak->setText(QString::number(output_tf.lomb.global.hrv.peakLF,'f',2));
    ui->label_lomb_hfPeak->setText(QString::number(output_tf.lomb.global.hrv.peakHF,'f',2));
    ui->label_lomb_vlfPower1->setText(QString::number(output_tf.lomb.global.hrv.aVLF,'f',2));
    ui->label_lomb_vlfPower2->setText(QString::number(output_tf.lomb.global.hrv.pVLF,'f',1));
    ui->label_lomb_lfPower1->setText(QString::number(output_tf.lomb.global.hrv.aLF,'f',2));
    ui->label_lomb_lfPower2->setText(QString::number(output_tf.lomb.global.hrv.pLF,'f',1));
    ui->label_lomb_lfPower3->setText(QString::number(output_tf.lomb.global.hrv.nLF,'f',3));
    ui->label_lomb_hfPower1->setText(QString::number(output_tf.lomb.global.hrv.aHF,'f',2));
    ui->label_lomb_hfPower2->setText(QString::number(output_tf.lomb.global.hrv.pHF,'f',1));
    ui->label_lomb_hfPower3->setText(QString::number(output_tf.lomb.global.hrv.nHF,'f',3));
    ui->label_lomb_lfhf->setText(QString::number(output_tf.lomb.global.hrv.LFHF,'f',3));

    ui->label_wavelet_vlfPeak->setText(QString::number(output_tf.wav.global.hrv.peakVLF,'f',2));
    ui->label_wavelet_lfPeak->setText(QString::number(output_tf.wav.global.hrv.peakLF,'f',2));
    ui->label_wavelet_hfPeak->setText(QString::number(output_tf.wav.global.hrv.peakHF,'f',2));
    ui->label_wavelet_vlfPower1->setText(QString::number(output_tf.wav.global.hrv.aVLF,'f',2));
    ui->label_wavelet_vlfPower2->setText(QString::number(output_tf.wav.global.hrv.pVLF,'f',1));
    ui->label_wavelet_lfPower1->setText(QString::number(output_tf.wav.global.hrv.aLF,'f',2));
    ui->label_wavelet_lfPower2->setText(QString::number(output_tf.wav.global.hrv.pLF,'f',1));
    ui->label_wavelet_lfPower3->setText(QString::number(output_tf.wav.global.hrv.nLF,'f',3));
    ui->label_wavelet_hfPower1->setText(QString::number(output_tf.wav.global.hrv.aHF,'f',2));
    ui->label_wavelet_hfPower2->setText(QString::number(output_tf.wav.global.hrv.pHF,'f',1));
    ui->label_wavelet_hfPower3->setText(QString::number(output_tf.wav.global.hrv.nHF,'f',3));
    ui->label_wavelet_lfhf->setText(QString::number(output_tf.wav.global.hrv.LFHF,'f',3));

    qDebug()<<"ComputeHRV()103";
    TFDataConvert *tfdc=new TFDataConvert(output_tf,ui->lineEdit_HFH->text().toDouble(),false);
    qDebug()<<"ComputeHRV()104";
    double burg_maxv,burg_minv;
    burg_psd.clear();
    burg_t.clear();
    burt_f.clear();
    burg_psd=tfdc->getBurgData(burg_t,burt_f,burg_maxv,burg_minv);
    qDebug()<<"ComputeHRV()104";
    timeFreqSpectrogramPlot_burg->setData(burg_t,burt_f,burg_psd,false,burg_maxv,0);
    qDebug()<<"ComputeHRV()105";
    double lomb_maxv,lomb_minv;
    lomb_PSD.clear();
    lomb_t.clear();
    lomb_F.clear();
    lomb_PSD=tfdc->getLSData(lomb_t,lomb_F,lomb_maxv,lomb_minv);
    timeFreqSpectrogramPlot_lomb->setData(lomb_t,lomb_F,lomb_PSD,false,lomb_maxv,0);

    qDebug()<<"ComputeHRV()106";
    double wav_maxv,wav_minv;
    wav_PSD.clear();
    wav_t.clear();
    wav_F.clear();
    wav_PSD=tfdc->getWaveletData(wav_t,wav_F,wav_maxv,wav_minv);
    timeFreqSpectrogramPlot_wavelet->setData(wav_t,wav_F,wav_PSD,true,wav_maxv,0);
    qDebug()<<"ComputeHRV()107";
    modifier->fill3DDataProxy(burg_t,burt_f,burg_psd);

    delete tfdc;
    qDebug()<<"ComputeHRV()11";
    getTimeFreqGlobalData(output_tf);
    qDebug()<<"ComputeHRV()12";
    getLFHF(output_tf,false);
    qDebug()<<"ComputeHRV()13";
    getMpInfo(output_tf,0);
    qDebug()<<"ComputeHRV()14";
}


void HRVAna::ComputeNonLinearIndex()
{
    int dwDataLen = proRRData_y.size();
    if (dwDataLen > 1) {
        double temp1 = 0;
        QVector<double>temp2;
        temp2.reserve(dwDataLen - 1);
        for (int i = 0; i < dwDataLen - 1; ++i) {
            temp1 += std::abs((proRRData_y[i + 1] - proRRData_y[i])*1000);
            temp2.emplace_back(1000*(std::sqrt(proRRData_y[i + 1] * proRRData_y[i + 1] + proRRData_y[i] * proRRData_y[i])));
        }
        double VAI = temp1 / ((double)dwDataLen - 1);
        double VLI = ArrayOperation::instance().calculateStdDev(temp2);
        ui->label_vai->setText(QString::number(VAI,'f',2));
        ui->label_vli->setText(QString::number(VLI,'f',2));
    }
}
/**
 * 异常rr间期检测
 * @brief HRVAna::EctopicPro
 */
void HRVAna::EctopicPro(bool b_p,bool b_sd,bool b_median)
{
    OutlierDetector detector;
    QVector<bool> outliers_p(rawrr_Len,  false);
    QVector<bool> outliers_median(rawrr_Len,  false);
    QVector<bool> outliers_sd(rawrr_Len,  false);
    if(b_p)
        outliers_p = detector.locateOutliers(rawRRData_x, rawRRData_y, "percent", ui->lineEdit_EctopicD_p->text().toDouble());

    if(b_sd)
        outliers_sd=detector.locateOutliers(rawRRData_x,rawRRData_y,"sd",ui->lineEdit_EctopicD_std->text().toDouble());

    if(b_median)
        outliers_median=detector.locateOutliers(rawRRData_x,rawRRData_y,"median",ui->lineEdit_EctopicD_median->text().toDouble());
    Ectopic_b.clear();Ectopic_pos.clear();
    for(int i=0;i<rawrr_Len;i++)
    {
        bool b=outliers_p.at(i)|| outliers_sd.at(i) || outliers_median.at(i);
        Ectopic_b.append(b);
        if(b)
            Ectopic_pos.append(i);
    }
}

/**
 * 异常心跳替换
 * @brief HRVAna::ReplaceEctopicPro
 */
void HRVAna::ReplaceEctopicPro()
{
    if(ui->radioButton_EctopicRep_None->isChecked())
    {
        //啥也不做
        proRRData_x.clear();proRRData_y.clear();
        proRRData_x=rawRRData_x;
        proRRData_y=rawRRData_y;
    }else if(ui->radioButton_EctopicRep_Mean->isChecked())
    {
        int meanv=ui->lineEdit_EctopicRep_mean->text().toInt();
        if(!Ectopic_pos.isEmpty())
        {
            if(meanv>0)
            {
                // 创建 replaceOutliers 对象
                replaceOutliers processor(rawRRData_x, rawRRData_y, Ectopic_b);
                // 设置替换方法为均值替换，假设窗口大小为 3（即左右各取 1 个点）
                processor.setMethod("mean",  meanv);
                // 执行替换
                auto result = processor.execute();
                proRRData_x.clear();proRRData_y.clear();
                proRRData_x=result.first;
                proRRData_y=result.second;

            }else{
                QMessageBox::about(this,"温馨提示","均值窗口不能为空");
            }


        }
    }else if(ui->radioButton_EctopicRep_Median->isChecked())
    {
        int medianv=ui->lineEdit_EctopicRep_median->text().toInt();
        if(!Ectopic_b.isEmpty())
        {
            if(medianv>0)
            {
                // 创建 replaceOutliers 对象
                replaceOutliers processor(rawRRData_x, rawRRData_y, Ectopic_b);
                // 设置替换方法为中值替换，假设窗口大小为 3（即左右各取 1 个点）
                processor.setMethod("median",  medianv);
                // 执行替换
                auto result = processor.execute();
                proRRData_x.clear();proRRData_y.clear();
                proRRData_x=result.first;
                proRRData_y=result.second;
            }else{
                QMessageBox::about(this,"温馨提示","中值窗口不能为空");
            }
        }
    }else if(ui->radioButton_EctopicRep_Spline->isChecked())
    {
        if(!Ectopic_b.isEmpty())
        {
            // 创建 replaceOutliers 对象
            replaceOutliers processor(rawRRData_x, rawRRData_y, Ectopic_b);
            processor.setMethod("cubic");
            // 执行替换
            auto result = processor.execute();
            proRRData_x.clear();proRRData_y.clear();
            proRRData_x=result.first;
            proRRData_y=result.second;
        }
    }else if(ui->radioButton_EctopicRep_ReMove->isChecked())
    {
        if(!Ectopic_b.isEmpty())
        {
            // 创建 replaceOutliers 对象
            replaceOutliers processor(rawRRData_x, rawRRData_y, Ectopic_b);
            processor.setMethod("remove");
            // 执行替换
            auto result = processor.execute();
            proRRData_x.clear();proRRData_y.clear();
            proRRData_x=result.first;
            proRRData_y=result.second;
            qDebug()<<"remove"<<proRRData_y.size();
        }
    }
}
void HRVAna::ProHistogram_RR()
{
    double minVal, maxVal;
    ArrayOperation::instance().maximum(rawRRData_y,minVal,maxVal);
    int numBins = (maxVal - minVal)*1000/128;
    numBins=10;
    // 计算直方图
    auto histogramNum = histogram_RR.computeHistogram(rawRRData_y, numBins, minVal, maxVal);
    auto binEdges = histogram_RR.computeBinEdges(minVal, maxVal, numBins);
    // 绘制直方图
    histogram_RR.plotHistogram(ui->widget_RRHistogram, histogramNum,"IBI Histogram","IBI(ms)","Counts");
}

void HRVAna::ProHistogram_HR()
{
    double minVal, maxVal;
    QVector<double>HrData;
    int len=rawRRData_y.size();
    for(int i=0;i<len;i++)
    {
        HrData.append(1000*60/rawRRData_y.at(i));
    }
    ArrayOperation::instance().maximum(HrData,minVal,maxVal);
    int numBins = (maxVal - minVal)*1000/128;
    numBins=10;
    // 计算直方图
    auto histogramNum = histogram_HR.computeHistogram(HrData, numBins, minVal, maxVal);
    auto binEdges = histogram_HR.computeBinEdges(minVal, maxVal, numBins);
    // 绘制直方图
    histogram_HR.plotHistogram(ui->widget_HRHistogram, histogramNum,"HR Histogram","HR(bpm)","Counts");
}

void HRVAna::on_comboBox_tf_type_currentIndexChanged(int index)
{
    if(index==0){
        ui->comboBox_surf->setVisible(false);
        ui->tabWidget_2->setCurrentIndex(0);
        TFDataConvert *tfdc=new TFDataConvert(output_tf,ui->lineEdit_HFH->text().toDouble(),false);
        QVector<double>burg_t,burt_f;
        double burg_maxv,burg_minv;
        QVector<QVector<double>>burg_psd=tfdc->getBurgData(burg_t,burt_f,burg_maxv,burg_minv);
        timeFreqSpectrogramPlot_burg->setData(burg_t,burt_f,burg_psd,false,burg_maxv,0);
        QVector<double>lomb_t,lomb_F;
        double lomb_maxv,lomb_minv;
        QVector<QVector<double>>lomb_PSD=tfdc->getLSData(lomb_t,lomb_F,lomb_maxv,lomb_minv);
        timeFreqSpectrogramPlot_lomb->setData(lomb_t,lomb_F,lomb_PSD,false,lomb_maxv,0);

        QVector<double>wav_t,wav_F;
        double wav_maxv,wav_minv;
        QVector<QVector<double>>wav_PSD=tfdc->getWaveletData(wav_t,wav_F,wav_maxv,wav_minv);
        timeFreqSpectrogramPlot_wavelet->setData(wav_t,wav_F,wav_PSD,true,wav_maxv,0);
        delete tfdc;
    }else if(index==1){
        ui->tabWidget_2->setCurrentIndex(0);
        ui->comboBox_surf->setVisible(false);
        TFDataConvert *tfdc=new TFDataConvert(output_tf,ui->lineEdit_HFH->text().toDouble(),true);
        QVector<double>burg_t,burt_f;
        double burg_maxv,burg_minv;
        QVector<QVector<double>>burg_psd=tfdc->getBurgData(burg_t,burt_f,burg_maxv,burg_minv);
        timeFreqSpectrogramPlot_burg->setData(burg_t,burt_f,burg_psd,false,burg_maxv,burg_minv);
        QVector<double>lomb_t,lomb_F;
        double lomb_maxv,lomb_minv;
        QVector<QVector<double>>lomb_PSD=tfdc->getLSData(lomb_t,lomb_F,lomb_maxv,lomb_minv);
        timeFreqSpectrogramPlot_lomb->setData(lomb_t,lomb_F,lomb_PSD,false,lomb_maxv,lomb_minv);

        QVector<double>wav_t,wav_F;
        double wav_maxv,wav_minv;
        QVector<QVector<double>>wav_PSD=tfdc->getWaveletData(wav_t,wav_F,wav_maxv,wav_minv);
        timeFreqSpectrogramPlot_wavelet->setData(wav_t,wav_F,wav_PSD,true,wav_maxv,wav_minv);
        delete tfdc;
    }else if(index==2)
    {
        ui->tabWidget_2->setCurrentIndex(1);
        ui->comboBox_surf->setVisible(true);
    }else if(index==3)
    {
        //global psd
        ui->tabWidget_2->setCurrentIndex(2);
        ui->comboBox_surf->setVisible(false);

    }else if(index==4)
    {
        //lf&hf曲线图
        getLFHF(output_tf,false);
        ui->tabWidget_2->setCurrentIndex(3);
        ui->comboBox_surf->setVisible(false);
    }else if(index==5){
        getLFHF(output_tf,true);
        ui->tabWidget_2->setCurrentIndex(3);
        ui->comboBox_surf->setVisible(false);
    }
}


void HRVAna::on_comboBox_surf_currentIndexChanged(int index)
{
    if(index==0)
    {
        modifier->fill3DDataProxy(burg_t,burt_f,burg_psd);
    }else if(index==1)
    {
        modifier->fill3DDataProxy(lomb_t,lomb_F,lomb_PSD);
    }else if(index==2)
    {
        modifier->fill3DDataProxy_wavelet(wav_t,wav_F,wav_PSD);
    }
}

void HRVAna::getTimeFreqGlobalData(struct9_T output_tf)
{
    DataConvert dc;
    double vlf_h=ui->lineEdit_VLFH->text().toDouble();
    double lf_l=ui->lineEdit_LFL->text().toDouble();
    double lf_h=ui->lineEdit_LFH->text().toDouble();
    double hf_l=ui->lineEdit_HFL->text().toDouble();
    double hf_h=ui->lineEdit_HFH->text().toDouble();
    QVector<double> wav_f,wav_psd;
    for(int i=0;i<449;i++)
    {
        wav_f.append(output_tf.wav.global.f[i]);
        wav_psd.append(output_tf.wav.global.psd[i]);
    }

    QVector<double>ar_f=dc.convertToQVector(output_tf.ar.global.f);
    QVector<double>ar_psd=dc.convertToQVector(output_tf.ar.global.psd);

    QVector<double>lomb_f=dc.convertToQVector(output_tf.lomb.global.f);
    QVector<double>lomb_psd=dc.convertToQVector(output_tf.lomb.global.psd);


    QVector<double>wav_f_vlf,wav_f_lf,wav_f_hf;
    QVector<double>wav_psd_vlf,wav_psd_lf,wav_psd_hf;

    QVector<double>ar_f_vlf,ar_f_lf,ar_f_hf;
    QVector<double>ar_psd_vlf,ar_psd_lf,ar_psd_hf;

    QVector<double>lomb_f_vlf,lomb_f_lf,lomb_f_hf;
    QVector<double>lomb_psd_vlf,lomb_psd_lf,lomb_psd_hf;

    for(int i=0;i<449;i++)
    {
        if(wav_f.at(i)<vlf_h){
            wav_f_vlf.append(wav_f.at(i));
            wav_psd_vlf.append(wav_psd.at(i));
        }
        if(wav_f.at(i)>=lf_l && wav_f.at(i)<lf_h){
            wav_f_lf.append(wav_f.at(i));
            wav_psd_lf.append(wav_psd.at(i));
        }
        if(wav_f.at(i)>=hf_l && wav_f.at(i)<hf_h){
            wav_f_hf.append(wav_f.at(i));
            wav_psd_hf.append(wav_psd.at(i));
        }
    }

    int PSDpoints=ui->lineEdit_PSDpoints->text().toInt();
    for(int i=0;i<PSDpoints;i++)
    {
        if(ar_f.at(i)<vlf_h){
            ar_f_vlf.append(ar_f.at(i));
            ar_psd_vlf.append(ar_psd.at(i));
        }
        if(ar_f.at(i)>=lf_l && ar_f.at(i)<lf_h){
            ar_f_lf.append(ar_f.at(i));
            ar_psd_lf.append(ar_psd.at(i));
        }
        if(ar_f.at(i)>=hf_l && ar_f.at(i)<hf_h){
            ar_f_hf.append(ar_f.at(i));
            ar_psd_hf.append(ar_psd.at(i));
        }

        if(lomb_f.at(i)<vlf_h){
            lomb_f_vlf.append(lomb_f.at(i));
            lomb_psd_vlf.append(lomb_psd.at(i));
        }
        if(lomb_f.at(i)>=lf_l && lomb_f.at(i)<lf_h){
            lomb_f_lf.append(lomb_f.at(i));
            lomb_psd_lf.append(lomb_psd.at(i));
        }
        if(lomb_f.at(i)>=hf_l && lomb_f.at(i)<hf_h){
            lomb_f_hf.append(lomb_f.at(i));
            lomb_psd_hf.append(lomb_psd.at(i));
        }


    }

    QVector<QVector<double>> wav_fData = {wav_f_vlf, wav_f_lf,wav_f_hf};
    QVector<QVector<double>> wav_psdData = {wav_psd_vlf,wav_psd_lf,wav_psd_hf};

    QVector<QVector<double>> ar_fData = {ar_f_vlf, ar_f_lf,ar_f_hf};
    QVector<QVector<double>> ar_psdData = {ar_psd_vlf,ar_psd_lf,ar_psd_hf};

    QVector<QVector<double>> lomb_fData = {lomb_f_vlf, lomb_f_lf,lomb_f_hf};
    QVector<QVector<double>> lomb_psdData = {lomb_psd_vlf,lomb_psd_lf,lomb_psd_hf};

    // 调用绘制
    PSDCurvesDrawUtil pdu;
    pdu.drawMultiCurves(ui->widget_global_wavelet,"Wavelet功率谱密度曲线图", wav_fData, wav_psdData);
    pdu.drawMultiCurves(ui->widget_global_burg,"AR功率谱密度曲线图", ar_fData, ar_psdData);
    pdu.drawMultiCurves(ui->widget_global_lomb,"lomb功率谱密度曲线图", lomb_fData, lomb_psdData);

}

void HRVAna::getLFHF(struct9_T output_tf,bool isratio)
{
    DataConvert dc;
    QVector<double>ar_lf=dc.convertToQVector(output_tf.ar.hrv.aLF);
    QVector<double>ar_hf=dc.convertToQVector(output_tf.ar.hrv.aHF);
    QVector<double>ar_lfhf=dc.convertToQVector(output_tf.ar.hrv.LFHF);
    QVector<QVector<double>>ar_t=dc.convertRowMajor(output_tf.ar.t);


    QVector<double>lomb_lf=dc.convertToQVector(output_tf.lomb.hrv.aLF);
    QVector<double>lomb_hf=dc.convertToQVector(output_tf.lomb.hrv.aHF);
    QVector<double>lomb_lfhf=dc.convertToQVector(output_tf.lomb.hrv.LFHF);
    QVector<double>lomb_t=dc.convertToQVector(output_tf.lomb.t);


    QVector<QVector<double>>wav_lf=dc.convertRowMajor(output_tf.wav.hrv.aLF);
    QVector<QVector<double>>wav_hf=dc.convertRowMajor(output_tf.wav.hrv.aHF);
    QVector<QVector<double>>wav_lfhf=dc.convertRowMajor(output_tf.wav.hrv.LFHF);
    QVector<QVector<double>>wav_t=dc.convertRowMajor(output_tf.wav.t);



    LFHFPlot lfhfplot;
    QVector<QString>lineNames;
    if(isratio)
        lineNames={"lf/hf"};
    else
        lineNames={"lf","hf"};
    QVector<QVector<double>>xDataList_ar,yDataList_ar;
    QVector<QVector<double>>xDataList_lomb,yDataList_lomb;
    QVector<QVector<double>>xDataList_wav,yDataList_wav;
    int ar_time_Len=ar_t.at(0).size();
    QVector<double>x_ar;
    for(int i=0;i<ar_time_Len;i++)
        x_ar.append(ar_t.at(0).at(i));

    if(isratio){
        xDataList_ar.append(x_ar);
        yDataList_ar.append(ar_lfhf);
    }else{
        xDataList_ar.append(x_ar);xDataList_ar.append(x_ar);
        yDataList_ar.append(ar_lf);yDataList_ar.append(ar_hf);
    }


    int lomb_time_Len=lomb_t.size();
    QVector<double>x_lomb;
    for(int i=0;i<lomb_time_Len;i++)
        x_lomb.append(lomb_t.at(i));

    if(isratio){
        xDataList_lomb.append(x_ar);
        yDataList_lomb.append(lomb_lfhf);
    }else{
        xDataList_lomb.append(x_ar);xDataList_lomb.append(x_ar);
        yDataList_lomb.append(lomb_lf);yDataList_lomb.append(lomb_hf);
    }



    int wav_time_Len=wav_t.at(0).size();

    QVector<double>x_wav;
    QVector<double>lf_wav;
    QVector<double>hf_wav;
    QVector<double>hfhf_wav;

    for(int i=0;i<wav_time_Len;i++){
        x_wav.append(wav_t.at(0).at(i));
        lf_wav.append(wav_lf.at(0).at(i));
        hf_wav.append(wav_hf.at(0).at(i));
        hfhf_wav.append(wav_lfhf.at(0).at(i));
    }

    if(isratio){
        xDataList_wav.append(x_wav);
        yDataList_wav.append(wav_lfhf);
    }else{
        xDataList_wav.append(x_wav);xDataList_wav.append(x_wav);
        yDataList_wav.append(wav_lf);yDataList_wav.append(wav_hf);
    }

    if(isratio)
    {
        lfhfplot.drawMultiCurves(ui->widget_burg_lfhf,"LF/HF（burg）变化趋势图", lineNames,true,xDataList_ar, yDataList_ar);
        lfhfplot.drawMultiCurves(ui->widget_lomb_lfhf,"LF/HF（lomb）变化趋势图", lineNames,true,xDataList_lomb, yDataList_lomb);
        lfhfplot.drawMultiCurves(ui->widget_wav_lfhf,"LF/HF（wavelet）变化趋势图", lineNames,true,xDataList_wav, yDataList_wav);
    }else{
        lfhfplot.drawMultiCurves(ui->widget_burg_lfhf,"LF-HF（burg）变化趋势图", lineNames,false,xDataList_ar, yDataList_ar);
        lfhfplot.drawMultiCurves(ui->widget_lomb_lfhf,"LF-HF（lomb）变化趋势图", lineNames,false,xDataList_lomb, yDataList_lomb);
        lfhfplot.drawMultiCurves(ui->widget_wav_lfhf,"LF-HF（wavelet）变化趋势图", lineNames,false,xDataList_wav, yDataList_wav);

    }


}
void HRVAna::getMpInfo(struct9_T output_tf,int type)
{
    DataConvert dc;
    QVector<double>lf,hf,lfhf,tp,t;
    QVector<double>emotion,mp,fatigure,rePressure;
    QVector<double>activity,ballance,heart;
    int age=ui->lineEdit_age->text().toInt();
    int sex=ui->comboBox_sex->currentIndex();

    if(type==0)
    {
        lf=dc.convertToQVector(output_tf.ar.hrv.aLF);
        hf=dc.convertToQVector(output_tf.ar.hrv.aHF);
        lfhf=dc.convertToQVector(output_tf.ar.hrv.LFHF);
        tp=dc.convertToQVector(output_tf.ar.hrv.aTotal);
        QVector<QVector<double>>ar_t=dc.convertRowMajor(output_tf.ar.t);
        int ar_time_Len=ar_t.at(0).size();
        for(int i=0;i<ar_time_Len;i++){
            t.append(ar_t.at(0).at(i));
            emotion.append(mentPressureComp->Cacul_Emotion(lfhf.at(i)));
            mp.append(mentPressureComp->Cal_MsiScore(lfhf.at(i),age,sex));
            fatigure.append(mentPressureComp->CalLfScore(lf.at(i),age,sex));
            activity.append(mentPressureComp->Cal_TpScore(tp.at(i),age,sex));
            ballance.append(mentPressureComp->Cal_ANSBalance(lf.at(i)*100/tp.at(i),age,sex));
            heart.append(mentPressureComp->CalHfScore(hf.at(i),age,sex));
        }

    }
    if(type==1)
    {
        lf=dc.convertToQVector(output_tf.lomb.hrv.aLF);
        hf=dc.convertToQVector(output_tf.lomb.hrv.aHF);
        lfhf=dc.convertToQVector(output_tf.lomb.hrv.LFHF);
        tp=dc.convertToQVector(output_tf.lomb.hrv.aTotal);
        t=dc.convertToQVector(output_tf.lomb.t);
        int t_len=t.size();
        for(int i=0;i<t_len;i++)
        {
            emotion.append(mentPressureComp->Cacul_Emotion(lfhf.at(i)));
            mp.append(mentPressureComp->Cal_MsiScore(lfhf.at(i),age,sex));
            fatigure.append(mentPressureComp->CalLfScore(lf.at(i),age,sex));
            activity.append(mentPressureComp->Cal_TpScore(tp.at(i),age,sex));
            ballance.append(mentPressureComp->Cal_ANSBalance(lf.at(i)*100/tp.at(i),age,sex));
            heart.append(mentPressureComp->CalHfScore(hf.at(i),age,sex));
        }
    }
    if(type==2)
    {
        QVector<QVector<double>>wav_lf=dc.convertRowMajor(output_tf.wav.hrv.aLF);
        QVector<QVector<double>>wav_hf=dc.convertRowMajor(output_tf.wav.hrv.aHF);
        QVector<QVector<double>>wav_lfhf=dc.convertRowMajor(output_tf.wav.hrv.LFHF);
        QVector<QVector<double>>wav_tp=dc.convertRowMajor(output_tf.wav.hrv.aTotal);
        QVector<QVector<double>>wav_t=dc.convertRowMajor(output_tf.wav.t);
        int wav_time_Len=wav_t.at(0).size();
        for(int i=0;i<wav_time_Len;i++){
            t.append(wav_t.at(0).at(i));
            lf.append(wav_lf.at(0).at(i));
            hf.append(wav_hf.at(0).at(i));
            emotion.append(mentPressureComp->Cacul_Emotion(wav_lfhf.at(0).at(i)));
            mp.append(mentPressureComp->Cal_MsiScore(wav_lfhf.at(0).at(i),age,sex));
            fatigure.append(mentPressureComp->CalLfScore(wav_lf.at(0).at(i),age,sex));
            activity.append(mentPressureComp->Cal_TpScore(wav_tp.at(0).at(i),age,sex));
            ballance.append(mentPressureComp->Cal_ANSBalance(wav_lf.at(0).at(i)*100/wav_tp.at(0).at(i),age,sex));
            heart.append(mentPressureComp->CalHfScore(wav_hf.at(0).at(i),age,sex));
        }
    }





    SingleCurveDraw singleCurveDraw;
    singleCurveDraw.draw(ui->widget_emotion,"情绪指数趋势图", "情绪指数",t, emotion);
    singleCurveDraw.draw(ui->widget_mp,"精神压力变化趋势图", "精神压力",t, mp);
    singleCurveDraw.draw(ui->widget_fatigure,"疲劳指数变化趋势图", "疲劳指数",t, mp);

    singleCurveDraw.draw(ui->widget_activity,"自主神经系统活性指数趋势图", "自主神经系统活性",t, activity);
    singleCurveDraw.draw(ui->widget_ballance,"自主神经系统平衡性指数趋势图", "自主神经系统平衡性",t, ballance);
    singleCurveDraw.draw(ui->widget_heart,"心脏稳定性指数趋势图", "心脏稳定性指数",t, heart);

}
void HRVAna::on_lineEdit_age_textEdited(const QString &arg1)
{
    int age=arg1.toInt();
    mentPressureComp->SetUserInfo(age,ui->comboBox_sex->currentIndex());
}


void HRVAna::on_comboBox_sex_currentIndexChanged(int index)
{
    int age=ui->lineEdit_age->text().toInt();
    mentPressureComp->SetUserInfo(age,index);
}


void HRVAna::on_comboBox_mp_currentIndexChanged(int index)
{
    getMpInfo(output_tf,index);
}

